package com.example.spring.jpa.specification;

import com.example.spring.jpa.ast.PriorityOperation;

import javax.persistence.criteria.*;
/**
 * @author: orange
 * @create: 2023-07-24 19:02
 * @description:
 */
public abstract class NumberGenerate extends GlobalPredicate {
    @Override
    public Predicate doGenerate(GenerateApplicationContext context) {

        Number number = getNumber(context.getRight());
        Expression<? extends Number> expression = getExpression(context);
        if (PriorityOperation.NotEquals$1.getOperation().equals(context.getOperation())
                || PriorityOperation.NotEquals$2.getOperation().equals(context.getOperation())) {
            return context.getBuilder().notEqual(expression, number);
        } else if (PriorityOperation.SaferEquals.getOperation().equals(context.getOperation())) {
            return context.getBuilder().equal(expression, number);
        } else if (PriorityOperation.GT.getOperation().equals(context.getOperation())) {
            return context.getBuilder().gt(expression, number);
        } else if (PriorityOperation.LT.getOperation().equals(context.getOperation())) {
            return context.getBuilder().lt(expression, number);
        } else if (PriorityOperation.Equals.getOperation().equals(context.getOperation())) {
            return context.getBuilder().equal(expression, number);
        }
        return childGenerate(context);
    }

    public abstract Number getNumber(String right);
    public abstract Expression<? extends Number> getExpression(GenerateApplicationContext context);
    public abstract Predicate childGenerate(GenerateApplicationContext context);
}
